/* 
 * @(#)ExceptionCaughtInterceptor.java    Created on 2009-11-3
 * Copyright (c) 2009 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package com.abc.demo.common.interceptor;

import org.apache.struts2.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.abc.demo.common.utils.ActionUtils;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 对异常捕获并根据条件进行包装的拦截器.<br>
 * 对于输出方式为JSON的Action, 捕获异常后会封装�? <code>JSONException</code>, 便于框架统一处理异常.
 * 
 * @author huangwj
 * @version $Revision: 1.0 $, $Date: 2009-11-3 下午06:37:09 $
 */
public class ExceptionCaughtInterceptor extends AbstractInterceptor {

    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 6701034263059574532L;

    private static Logger logger = LoggerFactory.getLogger(ExceptionCaughtInterceptor.class);

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        try {
            return invocation.invoke();
        }
        catch (Exception e) {
            logger.error("Action invoke error", e);

            if (ActionUtils.isJsonAction(invocation)) {
                throw new JSONException(e.getMessage(), e.getCause());
            }
            else {
                throw e;
            }
        }
    }

}
